<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>登录</title>

		<link href="css/gl/login.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="script/jquery/jquery-1.5.1.min.js">
</script>
		<script type="text/javascript">
$(function() {

	if (window.ActiveXObject) {
		$("#sp").css("margin-top", "6px");
		$("#mima").html("密&nbsp;&nbsp;&nbsp;&nbsp;码:");
		$("#bleft").css("margin-top", "0px");
		$("#bright").css("margin-top", "0px");
	}
	displayInfo ();
});
function displayInfo ()
{
	<%
	String info = "";
	if (request.getAttribute("error") != null)
	{
		info = request.getAttribute("error").toString();
	%>
	alert('<%=info%>');
	<%
	}
	%>
	GetLastUser();
}
function checkForm()
{
	if ($("#uname").val() == "")
	{
		alert("用户名不能为空！");
		return false;
	}
	if ($("#pwd").val() == "")
	{
		alert("密码不能为空！");
		return false;
	}
	SetPwdAndChk();
	document.f1.submit();
	return true;
}
function GetLastUser()
{
    var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
    var usr = GetCookie(id);
    if(usr != null)
    {
        document.getElementById('uname').value = usr;
    }
    else
    {
        document.getElementById('uname').value = "";
    }
    
    GetPwdAndChk();
}

//點擊登錄時觸發客戶端事件
function SetPwdAndChk()
{   
    //取用戶名
    var usr = document.getElementById('uname').value;
    //alert(usr);
    //將最後一個用戶信息寫入到Cookie
    SetLastUser(usr);  
    //如果記住密碼選項被選中          
    if(document.getElementById('rempwd').checked == true)
    {
        //取密碼值 
        var pwd = document.getElementById('pwd').value;   
        //alert(pwd);             
        var expdate = new Date();
        expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
        //將用戶名和密碼寫入到Cookie                
        SetCookie(usr,pwd, expdate);
    }
    else
    {
        //如果沒有選中記住密碼,則立即過期
        ResetCookie();
    }
}


function SetLastUser(usr)
{
    var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";            
    var expdate = new Date();
    //當前時間加上兩周的時間
    expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));                
    SetCookie(id, usr, expdate);
}

//用戶名失去焦點時調用該方法
function GetPwdAndChk()
{ 
    var usr = document.getElementById('uname').value;
    var pwd = GetCookie(usr); 
         
    
    if(pwd != null)
    {
        document.getElementById('rempwd').checked = true;
        document.getElementById('pwd').value = pwd;
    }
    else
    {
        document.getElementById('rempwd').checked = false;
        document.getElementById('pwd').value = "";
    }
}

//取Cookie的值
function GetCookie (name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) 
    {
        var j = i + alen;
        //alert(j);
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

function getCookieVal (offset)
{           
    var endstr = document.cookie.indexOf (";", offset);       
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));           
    
}


//寫入到Cookie
function SetCookie(name, value, expires)
{
    var argv = SetCookie.arguments;
    //本例中length = 3
    var argc = SetCookie.arguments.length;
             
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +     
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function ResetCookie()
{
    var usr = document.getElementById('uname').value;   
    var expdate = new Date();            
    SetCookie(usr, null, expdate);
}
</script>
	</head>

	<body>
		<div id="main">
			<div id="tleft"></div>
			<div id="tright">
				<form name="f1" action="gl/login.do" method="post"
					onkeydown="if(event.keyCode==13) checkForm();">
					<table cellpadding="0" cellspacing="0">
						<tr>
							<td id="ren">

							</td>
							<td>
								用户名:
							</td>
							<td>
								<input id="uname" name="uname"  style="padding-left:3px;" >
							</td>
						</tr>
						<tr>
							<td id="suo">

							</td>
							<td id="mima">
								密&nbsp;&nbsp;码:
							</td>
							<td>
								<input id="pwd" name="pwd" style="padding-left:3px;" type="password">
							</td>
						</tr>
						<tr>
							<td>

							</td>
							<td>

							</td>
							<td style="font-size: 13px;">
								<input id="rempwd" name="rempwd" type="checkbox">
								<span id="sp">记住密码</span>
							</td>
						</tr>
						<tr>
							<td>

							</td>
							<td>

							</td>
							<td style="padding-top: 20px; padding-left: 43px;">
								<img alt="" style="cursor: pointer;" src="img/gl/login/09.jpg" onclick="checkForm()"/>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div style="clear: both;"></div>
			<div id="bleft"></div>
			<div id="bright"></div>
		</div>
	</body>
</html>
